const {
	createToken,
	checkToken
} = require("./jwt.js");

function isRequiredParams(arr, data = {}) {
	if (!data) {
		return {
			err: true,
			msg: arr.join("、") + "是必传字段",
		};
	}
	const hasNo = [];
	arr.forEach((item) => {
		if (data[item] == undefined) {
			hasNo.push(item);
		}
	});
	if (hasNo.length !== 0) {
		return {
			err: true,
			msg: hasNo.join("、") + "是必传字段",
		};
	}
	return {
		err: false,
		msg: "",
	};
}

async function doSqlMiddle(fn = async () => {}) {
	try {
		const res = await fn();
		return res;
	} catch (err) {
		console.error(err);
		return {
			err: true,
		};
	}
}

module.exports = {
	isRequiredParams,
	doSqlMiddle,
	createToken,
	checkToken
}